package com.java.base.exercise.designmode.single;

import java.util.concurrent.TimeUnit;

/**
 * @Author RenPu
 * @Date 2021/6/21 9:35
 * @Version 1.0
 * @Description: 设计模式---单例设计模式V2.0  懒汉式汉式加载  存在多线程并发访问线程不安全的问题
 **/
public class Single_002 {

    private static Single_002 Instance;

    private Single_002() {
    }

    public static Single_002 getInstance() {
        if (Instance == null) {
            try {
                Thread.sleep(TimeUnit.SECONDS.toSeconds(1));

                //假如当前线程刚刚执行到这步，但是还没有对此对象初始化，第二个线程从逻辑判断进入此方法，会创建俩个对象的现象
                Instance = new Single_002();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
        return Instance;
    }


}
